(function() {
    "use strict";
    var reportController = function ($scope, $http,report_Service,projectstart_Service) {


        //所有产品列表
        $scope.allpros = [];
        //获取所有的产品信息
        $scope.prosGet = function (){
            projectstart_Service.allprojectsGet().success(function(data){
                $scope.allpros = data.result;
                /*if ($scope.allpros != null && $scope.allpros.length > 0){
                    $scope.getCommitByProductPerson($scope.allpros[0].productKey);
                }*/
            });
        };

        //加载jira 7 图
        $scope.getJiraBug = function(){
            report_Service.jiraIssue().success(function(data){
                if(true){
                    var jiraIssue = data.result;
                    // $scope.initTableForProcess(jiraIssue);
                    loadJiraInfo(jiraIssue);
                }
            });
        };

        $scope.getCommitByProduct = function(){
            report_Service.getCommitByProduct().success(function(data){
                if(data.result != null){
                    var result = data.result;
                    $scope.initTableForCommitByProduct(result);
                }
            });
        };

        $scope.getCommitByProductPerson = function(productKey){
            report_Service.getCommitByProductPerson({productKey:productKey}).success(function(data){
                if(data.result != null){
                    var result = data.result;
                    $scope.initTableForCommitByProductPerson(result);
                }
            });
        };

        $scope.getTestrailByProduct = function(){
            report_Service.getTestrailByProduct().success(function(data){
                if(data.result != null){
                    var result = data.result;
                    $scope.initTableForTestrailByProduct(result);
                }
            });
        };

        $scope.changeProdcut = function () {
            if ($scope.selectedPro != null){
                $scope.clearTable(["commitByProductPerson"]);
                $scope.getCommitByProductPerson($scope.selectedPro.productKey);
            }
        };

        $scope.loadData = function () {
            $scope.prosGet();
            $scope.getJiraBug();
            $scope.getCommitByProduct();
            $scope.getTestrailByProduct();
        };

        $scope.loadData();
        //jira 7 图
        var loadJiraInfo = function (jiraIssueList) {
            if (jiraIssueList == null || jiraIssueList == undefined || jiraIssueList.length == 0) {
                return;
            }
            var jiraIssueReopenX = [];
            var jiraIssueReopenData = [];
            var jiraIssueReopenTitle = "reopen状态的bug";
            var jiraIssueOpenX = [];
            var jiraIssueOpenData = [];
            var jiraIssueOpenTitle = "open状态的bug";
            var jiraIssueNewBugX = [];
            var jiraIssueNewBugData = [];
            var jiraIssueNewBugTitle = "新创的bug";
            var jiraIssueCloseBugX = [];
            var jiraIssueCloseBugData = [];
            var jiraIssueCloseBugTitle = "close的bug";
            var jiraIssueNewTypeBugX = [];
            var jiraIssueNewTypeBugData = [];
            var jiraIssueNewTypeBugTitle = "新特性的bug";
            var jiraIssue3TypeBugX = [];
            var jiraIssue3TypeBugData = [];
            var jiraIssue3TypeBugTitle = "新特性、任务、子任务的bug";
            var jiraIssue2TypeBugX = [];
            var jiraIssue2TypeBugData = [];
            var jiraIssue2TypeBugTitle = "缺陷、改进的bug";
            angular.forEach(jiraIssueList, function (item) {
                if (item.type == 'reopen' && jiraIssueReopenX.length < 6) {
                    jiraIssueReopenX.push(item.productKey);
                    jiraIssueReopenData.push(item.total);
                }
                if (item.type == 'open' && jiraIssueOpenX.length < 6) {
                    jiraIssueOpenX.push(item.productKey);
                    jiraIssueOpenData.push(item.total);
                }
                if (item.type == 'newbug' && jiraIssueNewBugX.length < 6) {
                    jiraIssueNewBugX.push(item.productKey);
                    jiraIssueNewBugData.push(item.total);
                }
                if (item.type == 'closebug' && jiraIssueCloseBugX.length < 6) {
                    jiraIssueCloseBugX.push(item.productKey);
                    jiraIssueCloseBugData.push(item.total);
                }
                if (item.type == '新特性bug' && jiraIssueNewTypeBugX.length < 6) {
                    jiraIssueNewTypeBugX.push(item.productKey);
                    jiraIssueNewTypeBugData.push(item.total);
                }
                if (item.type == '3种bug' && jiraIssue3TypeBugX.length < 6) {
                    jiraIssue3TypeBugX.push(item.productKey);
                    jiraIssue3TypeBugData.push(item.total);
                }
                if (item.type == '2种bug' && jiraIssue2TypeBugX.length < 6) {
                    jiraIssue2TypeBugX.push(item.productKey);
                    jiraIssue2TypeBugData.push(item.total);
                }
            });
            $("#barJiraIssueOpen").highcharts(bar(jiraIssueOpenTitle, jiraIssueOpenX, jiraIssueOpenData,350,600));
            $("#barJiraIssueReopen").highcharts(bar(jiraIssueReopenTitle, jiraIssueReopenX, jiraIssueReopenData,350,600));
            $("#barJiraIssueNewBug").highcharts(bar(jiraIssueNewBugTitle, jiraIssueNewBugX, jiraIssueNewBugData,350,600));
            $("#barJiraIssueCloseBug").highcharts(bar(jiraIssueCloseBugTitle, jiraIssueCloseBugX, jiraIssueCloseBugData,350,600));
            $("#barJiraIssueNewTypeBug").highcharts(bar(jiraIssueNewTypeBugTitle, jiraIssueNewTypeBugX, jiraIssueNewTypeBugData,350,600));
            $("#barJiraIssue3TypeBug").highcharts(bar(jiraIssue3TypeBugTitle, jiraIssue3TypeBugX, jiraIssue3TypeBugData,350,600));
            $("#barJiraIssue2TypeBug").highcharts(bar(jiraIssue2TypeBugTitle, jiraIssue2TypeBugX, jiraIssue2TypeBugData,350,600));
        }

        $scope.chart2 = new h5Charts.SerialChart();
        $scope.initTableForCommitByProduct = function (list) {
            if (list == null || list == undefined || list.length == 0) {
                return;
            }

            var data = [];
            var colorList = [];
            angular.forEach(list, function (item) {
                var obj = {};
                obj.name = item.productName;
                obj.num = item.commitNumber;
                data.push(obj);
                colorList.push("#00B47A");

            });

            $scope.chart2.dataProvider = data;
            $scope.chart2.categoryField = "name";
            $scope.chart2.valueField = "num";
            $scope.chart2.type = "column";
            $scope.chart2.columnWidth = 20;
            $scope.chart2.colors = colorList;
            $scope.chart2.addTo("commitByProduct");

        };

        $scope.chart3 = new h5Charts.SerialChart();
        $scope.initTableForCommitByProductPerson = function (list) {
            if (list == null || list == undefined || list.length == 0) {
                return;
            }

            var data = [];
            var colorList = [];
            angular.forEach(list, function (item) {
                var obj = {};
                obj.name = item.authorName;
                obj.num = item.commitNumber;
                data.push(obj);
                colorList.push("#00B47A");

            });

            $scope.chart3.dataProvider = data;
            $scope.chart3.categoryField = "name";
            $scope.chart3.valueField = "num";
            $scope.chart3.type = "column";
            $scope.chart3.columnWidth = 20;
            $scope.chart3.colors = colorList;
            $scope.chart3.addTo("commitByProductPerson");

        };

        $scope.chart4 = new h5Charts.SerialChart();
        $scope.initTableForTestrailByProduct = function (list) {
            if (list == null || list == undefined || list.length == 0) {
                return;
            }

            var data = [];
            var colorList = [];
            angular.forEach(list, function (item) {
                var obj = {};
                obj.name = item.productName;
                obj.num = item.runNumber;
                data.push(obj);
                colorList.push("#00B47A");

            });

            $scope.chart4.dataProvider = data;
            $scope.chart4.categoryField = "name";
            $scope.chart4.valueField = "num";
            $scope.chart4.type = "column";
            $scope.chart4.columnWidth = 20;
            $scope.chart4.colors = colorList;
            $scope.chart4.addTo("testrailByProduct");

        };

        //清空画布内容的方法
        $scope.clearTable = function (divIds) {
            angular.forEach(divIds, function (item) {
                var canvas = document.getElementById(item);
                var cxt = canvas.getContext("2d");
                cxt.clearRect(0, 0, canvas.width, canvas.height);
            });
        };
    };

    angular.module("cicd.report",[])
        .controller("ReportController",["$scope","$http","report_Service","projectstart_Service",reportController]);
})();